package com.authorwjf;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity implements SensorEventListener {

	ArrayList<Float> mLastX = new  ArrayList<Float>();
	ArrayList<Float> mLastY = new  ArrayList<Float>();
	ArrayList<Float> mLastZ = new  ArrayList<Float>();
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

	private boolean mInitialized,file;
	private SensorManager mSensorManager;
	private Sensor mAccelerometer;
	private final float NOISE = (float) 2.0;
	private static final String LOG_TAG = "AudioRecordTest";
	private static String mFileName = null;

	private Button mRecordButton = null;
	private MediaRecorder mRecorder = null;

	private Button mPlayButton = null;
	private MediaPlayer mPlayer = null;
	boolean mStartRecording = true;
	boolean mStartPlaying = true;
	int i =0;
	private void onRecord(boolean start) {
		if (start) {
			startRecording();
		} else {
			stopRecording();
		}
	}

	private void onPlay(boolean start) {
		if (start) {
			startPlaying();
		} else {
			stopPlaying();
		}
	}

	private void startPlaying() {
		mPlayer = new MediaPlayer();
		try {
			mPlayer.setDataSource(mFileName);
			mPlayer.prepare();
			mPlayer.start();
		} catch (IOException e) {
			Log.e(LOG_TAG, "prepare() failed startPlaying");
		}
	}

	private void stopPlaying() {
		mPlayer.release();
		mPlayer = null;
	}

	private void startRecording() {
		mRecorder = new MediaRecorder();
		mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		mRecorder.setOutputFile(mFileName);
		mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

		try {
			mRecorder.prepare();
		} catch (IOException e) {
			Log.e(LOG_TAG, "prepare() failed startRecording");
		}

		mRecorder.start();
	}

	private void stopRecording() {
		mRecorder.stop();
		mRecorder.release();
		mRecorder = null;
	}

	public Main() {

		
	
		mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
		 file = (new File(mFileName,"AccelAudio")).mkdir();
		 if (!file) {
				Log.e(LOG_TAG, "mkdir() failed creating dir");
		    }
		mFileName = Environment.getExternalStorageDirectory().getAbsolutePath()+"/AccelAudio";

		mFileName += "/audiorecordtest.3gp";
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mInitialized = false;

		mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		mAccelerometer = mSensorManager
				.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		mSensorManager.registerListener(this, mAccelerometer,
				SensorManager.SENSOR_DELAY_NORMAL);
	}

	protected void onResume() {
		super.onResume();
		mSensorManager.registerListener(this, mAccelerometer,
				SensorManager.SENSOR_DELAY_NORMAL);
	}

	protected void onPause() {
		super.onPause();
		mSensorManager.unregisterListener(this);
		if (mRecorder != null) {
			mRecorder.release();
			mRecorder = null;
		}

		if (mPlayer != null) {
			mPlayer.release();
			mPlayer = null;
		}
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// can be safely ignored for this demo
	}
	public void GoPlayer(View view) {
		  Intent intent = new Intent(this, Player.class);
		  intent.putExtra(EXTRA_MESSAGE, mFileName);
		  startActivity(intent);

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		TextView tvX = (TextView) findViewById(R.id.x_axis);
		TextView tvY = (TextView) findViewById(R.id.y_axis);
		TextView tvZ = (TextView) findViewById(R.id.z_axis);
		mRecordButton = (Button) findViewById(R.id.button1);		
		mRecordButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				
				onRecord(mStartRecording);
				if (mStartRecording) {
					mRecordButton.setText("Stop recording");
				} else {
					mRecordButton.setText("Start recording");
				}
				mStartRecording = !mStartRecording;
			}
		});
		mPlayButton = (Button) findViewById(R.id.button2);
		mPlayButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {


				onPlay(mStartPlaying);
				if (mStartPlaying) {
					mPlayButton.setText("Stop playing");
				System.out.println("sdjkfhksdja");
					
				} else {
					mPlayButton.setText("Start playing");
				}
				mStartPlaying = !mStartPlaying;
			}
		});
		float x = event.values[0];
		float y = event.values[1];
		float z = event.values[2];

		if (!mInitialized) {
			mLastX.add(x);
			mLastY.add(y);
			mLastZ.add(z);

			tvX.setText("0.0");
			tvY.setText("0.0");
			tvZ.setText("0.0");

			mInitialized = true;
		} else {
			float deltaX = Math.abs(mLastX.get(mLastX.size()-1) - x);
			float deltaY = Math.abs(mLastY.get(mLastY.size()-1) - y);
			float deltaZ = Math.abs(mLastZ.get(mLastZ.size()-1) - z);

			if (deltaX < NOISE)
				deltaX = (float) 0.0;
			if (deltaY < NOISE)
				deltaY = (float) 0.0;
			if (deltaZ < NOISE)
				deltaZ = (float) 0.0;
			if(!mStartRecording){
				mLastX.add(x);
				mLastY.add(y);
				mLastZ.add(z);
				
				tvX.setText(Float.toString(deltaX));
				tvY.setText(Float.toString(deltaY));
				tvZ.setText(Float.toString(deltaZ));
			}
			
		}
	}
}





